package com.liangzhong.modules.bag.mediator
{
	import com.common.queueloader.QueueLoader;
	import com.common.queueloader.QueueLoaderEvent;
	import com.common.queueloader.QueueVo;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.constants.ApplicationConstants;
	import com.liangzhong.constants.NotificationConstants;
	import com.liangzhong.constants.ResourceConst;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.BagEvent;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.manager.ResourceManager;
	import com.liangzhong.mediator.ModuleMediator;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.type.BuildingType;
	import com.liangzhong.proxyconstant.bag.BagConst;
	import com.lz.football.module.bag.IBagView;
	
	import flash.display.DisplayObject;
	
	import lz.controls.managers.PopUpManager;
	
	import org.puremvc.as3.interfaces.INotification;

	/**
	 *背包 
	 * @author lake
	 * 
	 */	
	public class BagMediator extends ModuleMediator
	{
		public static const MediatorName :String = "BagMediator";
		
		public function BagMediator(mediatorName:String=null, viewComponent:Object=null, param:Object=null)
		{
			super(mediatorName, viewComponent, param);
		}
		/**
		 *获取视图 
		 * @return 
		 * 
		 */		
		public function get view():IBagView
		{
			return viewComponent as IBagView;
		}
		/**
		 *添加事件 
		 * 
		 */		
		override public function addEventListeners():void
		{
			(view as DisplayObject).addEventListener(ModuleEvent.REMOVE_MODULE,onRemoveModuleHandler);
			(view as DisplayObject).addEventListener(BagEvent.OPEN_CELLS,openCellHandler);
			(view as DisplayObject).addEventListener(BagEvent.GRADE_UP,gradeUpHandler);
			(view as DisplayObject).addEventListener(BagEvent.RESOLVE_ITEM,resolveHandler);
			(view as DisplayObject).addEventListener(ModuleEvent.DROP_TIME,dropTimeHandler);
		}
		/**
		 *删除处理 
		 * @param e
		 * 
		 */		
		private function onRemoveModuleHandler(e :ModuleEvent):void
		{
			removeMediator();
		}
		/**
		 *删除事件 
		 * 
		 */		
		override public function removeEventListeners():void
		{
			(view as DisplayObject).removeEventListener(ModuleEvent.REMOVE_MODULE,onRemoveModuleHandler);
			(view as DisplayObject).removeEventListener(BagEvent.OPEN_CELLS,openCellHandler);
			(view as DisplayObject).removeEventListener(BagEvent.GRADE_UP,gradeUpHandler);
			(view as DisplayObject).removeEventListener(BagEvent.RESOLVE_ITEM,resolveHandler);
			(view as DisplayObject).removeEventListener(ModuleEvent.DROP_TIME,dropTimeHandler);
		}
		/**
		 *开格子 
		 * @param e
		 * 
		 */		
		private function openCellHandler(e :BagEvent):void
		{
			this.sendNotification(ApplicationConstants.BAG_OPEN_CELLS);
		}
		/**
		 *升级
		 * @param e
		 * 
		 */		
		private function gradeUpHandler(e :BagEvent):void
		{
			this.sendNotification(ApplicationConstants.BAG_GRADE_UP);
		}
		/**
		 *分解 
		 * @param e
		 * 
		 */		
		private function resolveHandler(e :BagEvent):void
		{
			this.sendNotification(ApplicationConstants.BAG_RESOLVE_ITEM,e.param);
		}
		/**
		 *去除冷却时间 
		 * @param e
		 * 
		 */		
		private function dropTimeHandler(e :ModuleEvent):void
		{
			this.sendNotification(ApplicationConstants.Public_Drop_Time,e.param);
		}
		/**
		 *删除mediator 
		 * 
		 */		
		override public function removeMediator():void
		{
			super.removeMediator();
			facade.removeMediator(MediatorName);
		}
		/**
		 *注册信息 
		 * @return 
		 * 
		 */		
		override public function listNotificationInterests():Array
		{
			return [BagConst.GRADE_UP_RESULT,
				BagConst.OPEN_CELLS_RESULT,
				NotificationConstants.COMMON_DROP_TIME];
		}
		/**
		 *捕获信息 
		 * @param notification
		 * 
		 */		
		override public function handleNotification(notification:INotification):void
		{
			var retCode:int ;
			switch(notification.getName())
			{
				case BagConst.GRADE_UP_RESULT://升级
				{
					view.gradeUp();
					break;
				}
				case BagConst.OPEN_CELLS_RESULT://开格子
				{
					view.openCells();
					break;
				}
				case NotificationConstants.COMMON_DROP_TIME://冷却时间去除
				{
					if(notification.getBody() == BuildingType.BAG)
					{
						AppData.getInstance().bagDataManager.bagInfo.pretime = 0;
						view.dropTime();
					}
					break;
				}
			}
		}
		/**
		 * 注册
		 */		
		override public function onRegister():void
		{
			if(ResourceManager.instance.getResource(ResourceConst.BAG_RESOURCE))
			{
				showUI();
			}
			else
			{
				loader = new QueueLoader();
				var config :XML = XML(ResourceManager.instance.getResource(ResourceConst.MODULE_RES_CONFIG)).bag[0];
				bytesTotal = int(config.@totalBytes);
				var releaseVersion :String;
				for each(var item :XML in config.item)
				{
					releaseVersion = "?" + item.@version + "." + item.@itemType;
					loader.addQueue(new QueueVo(item.@resName,item.@id,item.@id),item.@url + releaseVersion);
				}
				loadRes();
			}
		}
		/**
		 *加载资源完毕 
		 * @param e
		 * 
		 */		
		override protected function onLoadComplete(e:QueueLoaderEvent):void
		{
			showUI();
			super.onLoadComplete(e);
			ResourceManager.instance.putResource(ResourceConst.BAG_RESOURCE,resList)
		}
		/**
		 *显示ui 
		 * 
		 */		
		private function showUI():void
		{
			viewComponent = Reflection.createInstance("com.lz.football.module.bag.BagView");
			view.initUI();
			PopUpManager.addPopUp(view as DisplayObject,GlobalDef.GameWindowContainer,GlobalDef.GameStage.stageWidth,GlobalDef.GameStage.stageWidth,false);
			addEventListeners();
			//设置缓存位图
			setCacheAsBitmap(true);
		}
		/**
		 * 删除
		 */		
		override public function onRemove():void
		{
			super.onRemove();
			removeEventListeners();
			view.destroyView();
		}
	}
}